package java6699.lesson08;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.io.File;
import java.io.FilenameFilter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;


/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-10-26 20:41
 **/
public class FileFilter6699Controller {
    @FXML
    public TextField tfDir;
    @FXML
    public Label lblMsg;
    @FXML
    public ListView<File> lvFiles;
    @FXML
    public ToggleGroup tg;

    private ObservableList<File> list;

    @FXML
    private void initialize(){ list = lvFiles.getItems();}

    @FXML
    public void listFile(ActionEvent actionEvent) {
        //获取文本框内容
        String sdir = tfDir.getText().trim();
        //根据输入的路劲， 创建正确的目录
        if (sdir.isEmpty()) {
            sdir = ".";
        }else {
            if (!new File(sdir).isDirectory()) {
                sdir = null;
            }else if (sdir.matches("[a-zA-Z]:")){
                sdir +=".";
            }
        }
        File dir = new File(sdir);
        if (dir == null) {
            lblMsg.setText("不是目录！!");
            tfDir.selectAll();
            tfDir.requestFocus();
            return;
        }else {
            tfDir.setText(dir.getPath());
            lblMsg.setText("");
        }
        //随机选择
        RadioButton rb = (RadioButton) tg.getSelectedToggle();
        String type = rb.getText().substring(0,3);
        switch (type){
            case "方法一"://匿名内部类
                inner6699(dir);
                break;
            case "方法二"://Lambda表达式
                lamabda6699(dir);
                break;
            case "方法三":
                reference6699(dir);
                break;
        }

    }
//用方法引用实现文件过滤器接口FilenameFilter
    private void reference6699(File dir) {
        //方法引用
        fillView6699(dir,this::suffix,this::size,this::modified);
    }
//3天前到现在修改过的文件
    private boolean modified(File dir, String s) {
        LocalDateTime ld = LocalDateTime.now().minusDays(3);
        long milli = ld.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return new File(dir,s).lastModified() > milli;
    }
//大于2kb的文件
    private boolean size(File file, String s) {
        return new File(file, s).length() / 1024 >=2;
    }
//后缀名为java的文件
    private boolean suffix(File file, String s) {
        return  s.endsWith(".java");
    }
//用Lambda表达式实现文件过滤器接口FilenameFilter
    private void lamabda6699(File dir) {
        //Lambda表达式
        LocalDateTime ld = LocalDateTime.now().minusDays(3);
        long milli = ld.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        fillView6699(dir,
                (d, name) -> name.endsWith(".java"),
                (d, name) -> new File(dir,name).length() / 1024 >=2,
                (d, name) -> new File(dir,name).lastModified() >milli);
    }
//用匿名内部类实现文件过滤器接口FilenameFilter
    private void inner6699(File dir) {
        FilenameFilter filter1 = new FilenameFilter(){
            //匿名内部类
            public  boolean accept(File dir, String name) {
                return name.endsWith(".java");
            }
        };
        FilenameFilter filter2 = new FilenameFilter(){
            public boolean accept(File dir, String name){
                return new File(dir,name).length() /1024 >=2;
            }
        };
        FilenameFilter filter3 = new FilenameFilter(){
            public boolean accept(File dir, String name){
                LocalDateTime ld = LocalDateTime.now().minusDays(3);
                long milli = ld.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                return new File(dir,name).lastModified() > milli;
            }
        };
        fillView6699(dir,filter1,filter2,filter3);
    }


    private void fillView6699(File dir, FilenameFilter filter1, FilenameFilter filter2, FilenameFilter filter3) {
        list.clear();
        FilenameFilter[] filters = {filter1,filter2,filter3};
        String[] msgs = {"(1)后缀为java的文件：","(2)大于2kb的文件：","(3)3天前到现在修改过的文件："};
        for (int j = 0; j < msgs.length; j++) {
            File[] files = dir.listFiles(filters[j]);
            list.add(new File(msgs[j] + files.length));
            list.addAll(Arrays.asList(files));
        }
    }
}
